package com.burstly.lib.util;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import com.burstly.lib.component.networkcomponent.AbstractAdaptorFactory;
import com.burstly.lib.feature.FeatureFactory;
import com.burstly.lib.feature.networks.IAdaptorFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public final class ManifestValidator
{
  private static final String BURSTLY_XML_SCHEME = "http://www.burstly.com/scheme";
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = ManifestValidator.class.getSimpleName();
  private static boolean sIsValidated;
  private static List<ValidationResults> sResults;

  private static List<String> checkActivities(PackageInfo paramPackageInfo, List<String> paramList)
  {
    if ((paramPackageInfo.activities != null) && (paramList != null))
      paramList = validate(paramList, selectActivityNames(paramPackageInfo.activities));
    return paramList;
  }

  private static List<String> checkPermissions(PackageInfo paramPackageInfo, List<String> paramList)
  {
    if ((paramPackageInfo.requestedPermissions != null) && (paramList != null))
      paramList = validate(paramList, Arrays.asList(paramPackageInfo.requestedPermissions));
    return paramList;
  }

  public static ValidationResults customValidate(Context paramContext, List<String> paramList1, List<String> paramList2, List<String> paramList3)
    throws PackageManager.NameNotFoundException
  {
    String str = paramContext.getPackageName();
    PackageInfo localPackageInfo = paramContext.getPackageManager().getPackageInfo(str, 4097);
    ValidationResults localValidationResults = new ValidationResults(paramList1, paramList2, paramList3);
    if (localPackageInfo != null)
    {
      localValidationResults.setMandatoryActivities(checkActivities(localPackageInfo, paramList1));
      localValidationResults.setMandatoryPermissions(checkPermissions(localPackageInfo, paramList2));
      localValidationResults.setOptionalPermissions(checkPermissions(localPackageInfo, paramList3));
      return localValidationResults;
    }
    LOG.logWarning(TAG, "Could not read package info. Package info is null.", new Object[0]);
    return localValidationResults;
  }

  public static ValidationResults customValidate(List<String> paramList1, List<String> paramList2, List<String> paramList3)
    throws PackageManager.NameNotFoundException
  {
    return customValidate(Utils.getApplicationContext(), paramList1, paramList2, paramList3);
  }

  public static IAdaptorFactory getFactory(String paramString)
  {
    try
    {
      IAdaptorFactory localIAdaptorFactory = FeatureFactory.getAdaptorFactoryFeature(paramString);
      if (localIAdaptorFactory == null)
        LOG.logInfo(TAG, "No {0} factory in config.xml. Check the configuration file.", new Object[] { paramString });
      return localIAdaptorFactory;
    }
    finally
    {
    }
  }

  private static void init(Context paramContext)
  {
    Utils.setApplicationContext(paramContext);
    sResults = new ArrayList();
  }

  private static boolean isMisconfigured(ValidationResults paramValidationResults)
  {
    List localList1 = paramValidationResults.getMandatoryActivities();
    if ((localList1 != null) && (!localList1.isEmpty()));
    List localList2;
    do
    {
      return true;
      localList2 = paramValidationResults.getMandatoryPermissions();
    }
    while ((localList2 != null) && (!localList2.isEmpty()));
    return false;
  }

  public static String logAllValidationFailures(ValidationResults paramValidationResults, String paramString)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append(logValidationFailures(paramValidationResults.getMandatoryActivities(), paramString + " activity", true));
    localStringBuilder.append(logValidationFailures(paramValidationResults.getMandatoryPermissions(), paramString + " permission", true));
    logValidationFailures(paramValidationResults.getOptionalPermissions(), paramString + " permission", false);
    if (localStringBuilder.length() > 0)
    {
      localStringBuilder.insert(0, "A list of critical manifest errors for " + paramString + ":\n");
      localStringBuilder.append("\n\n");
    }
    return localStringBuilder.toString();
  }

  private static String logValidationFailures(List<String> paramList, String paramString, boolean paramBoolean)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if (paramList != null)
    {
      Iterator localIterator = paramList.iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        if (paramBoolean)
        {
          localStringBuilder.append(LOG.logError(TAG, "Mandatory {0} is missing in AndroidManifest.xml: {1}", new Object[] { paramString, str }));
          localStringBuilder.append("\n");
        }
        else
        {
          LOG.logInfo(TAG, "Optional {0} is missing in AndroidManifest.xml: {1}", new Object[] { paramString, str });
        }
      }
    }
    return localStringBuilder.toString();
  }

  private static List<String> parseConfig(Context paramContext)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      XmlPullParserFactory localXmlPullParserFactory = XmlPullParserFactory.newInstance();
      localXmlPullParserFactory.setNamespaceAware(true);
      localXmlPullParserFactory.setValidating(false);
      XmlPullParser localXmlPullParser = localXmlPullParserFactory.newPullParser();
      localXmlPullParser.setInput(paramContext.getAssets().open("config.xml"), "utf-8");
      while (localXmlPullParser.getEventType() != 1)
      {
        if ((localXmlPullParser.getEventType() == 2) && (localXmlPullParser.getName().equals("bean")))
        {
          String str2 = localXmlPullParser.getAttributeValue(0);
          if (Boolean.parseBoolean(localXmlPullParser.getAttributeValue("http://www.burstly.com/scheme", "validate")))
            localArrayList.add(str2);
        }
        localXmlPullParser.next();
      }
    }
    catch (XmlPullParserException localXmlPullParserException)
    {
      LOG.logError(TAG, "Parse error config.xml. Check the file integrity.", new Object[0]);
      LoggerExt localLoggerExt = LOG;
      String str1 = TAG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = localXmlPullParserException.getMessage();
      localLoggerExt.logDebug(str1, "Detalis: {0}", arrayOfObject);
      return localArrayList;
    }
    catch (IOException localIOException)
    {
      LOG.logError(TAG, "Can not open config.xml file.", new Object[0]);
    }
    return localArrayList;
  }

  private static List<String> selectActivityNames(ActivityInfo[] paramArrayOfActivityInfo)
  {
    ArrayList localArrayList = new ArrayList(paramArrayOfActivityInfo.length);
    int i = paramArrayOfActivityInfo.length;
    for (int j = 0; j < i; j++)
      localArrayList.add(paramArrayOfActivityInfo[j].name);
    return localArrayList;
  }

  private static List<String> validate(List<String> paramList1, List<String> paramList2)
  {
    ArrayList localArrayList = new ArrayList(paramList1.size());
    Iterator localIterator = paramList1.iterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      if (!paramList2.contains(str))
        localArrayList.add(str);
    }
    return localArrayList;
  }

  public static void validate(Context paramContext)
  {
    StringBuilder localStringBuilder;
    if (!sIsValidated)
    {
      i = 0;
      try
      {
        init(paramContext);
        Iterator localIterator1 = parseConfig(paramContext).iterator();
        while (localIterator1.hasNext())
        {
          AbstractAdaptorFactory localAbstractAdaptorFactory = (AbstractAdaptorFactory)getFactory((String)localIterator1.next());
          if (localAbstractAdaptorFactory != null)
          {
            ValidationResults localValidationResults2 = localAbstractAdaptorFactory.validateManifest();
            sResults.add(localValidationResults2);
            if (i != 0)
              break label200;
            if (!isMisconfigured(localValidationResults2))
              break label205;
            break label200;
          }
        }
        localStringBuilder = new StringBuilder();
        Iterator localIterator2 = sResults.iterator();
        while (localIterator2.hasNext())
        {
          ValidationResults localValidationResults1 = (ValidationResults)localIterator2.next();
          localStringBuilder.append(logAllValidationFailures(localValidationResults1, localValidationResults1.getNetworkName()));
        }
      }
      catch (PackageManager.NameNotFoundException localNameNotFoundException)
      {
        LOG.logThrowable(TAG, localNameNotFoundException);
      }
    }
    else
    {
      return;
    }
    if (i != 0)
      throw new IllegalStateException("Mandatory activities or permissions not met!\n" + localStringBuilder);
    sResults = null;
    sIsValidated = true;
    return;
    label200: label205: for (int i = 1; ; i = 0)
      break;
  }

  public static class ValidationResults
  {
    private List<String> mMandatoryActivities;
    private List<String> mMandatoryPermissions;
    private String mNetworkName;
    private List<String> mOptionalPermissions;

    ValidationResults(List<String> paramList1, List<String> paramList2, List<String> paramList3)
    {
      this.mMandatoryActivities = paramList1;
      this.mMandatoryPermissions = paramList2;
      this.mOptionalPermissions = paramList3;
    }

    public List<String> getMandatoryActivities()
    {
      return this.mMandatoryActivities;
    }

    public List<String> getMandatoryPermissions()
    {
      return this.mMandatoryPermissions;
    }

    public String getNetworkName()
    {
      return this.mNetworkName;
    }

    public List<String> getOptionalPermissions()
    {
      return this.mOptionalPermissions;
    }

    void setMandatoryActivities(List<String> paramList)
    {
      this.mMandatoryActivities = paramList;
    }

    void setMandatoryPermissions(List<String> paramList)
    {
      this.mMandatoryPermissions = paramList;
    }

    public void setNetworkName(String paramString)
    {
      this.mNetworkName = paramString;
    }

    void setOptionalPermissions(List<String> paramList)
    {
      this.mOptionalPermissions = paramList;
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.util.ManifestValidator
 * JD-Core Version:    0.6.2
 */